package com.example.myapptwo.views;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myapptwo.R;
import com.example.myapptwo.bean.Student;
import com.example.myapptwo.database.DBHelper;
import com.example.myapptwo.utils.SpUtils;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

   private EditText mEtUsername;
   private EditText mEtPassword;
   private Button mBtnRegister;
   private Button mBtnLogin;
   private SpUtils mSpUtils;
   private EditText etStudentPassword;
   private ImageView ivShowHidePassword;


   /**
    * 切换密码的可见性
    */
   private void togglePasswordVisibility() {
      int currentInputType = etStudentPassword.getInputType();
      if ((currentInputType & InputType.TYPE_TEXT_VARIATION_PASSWORD) == InputType.TYPE_TEXT_VARIATION_PASSWORD) {
         // 密码当前是隐藏的，切换为可见
         etStudentPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
         ivShowHidePassword.setImageResource(R.drawable.ic_eye_open); // 设置图标为睁眼状态
      } else {
         // 密码当前是可见的，切换为隐藏
         etStudentPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
         ivShowHidePassword.setImageResource(R.drawable.ic_eye_closed); // 设置图标为闭眼状态
      }
      etStudentPassword.setSelection(etStudentPassword.getText().length()); // 将光标移动到文本末尾
   }



   @SuppressLint("ResourceType")
   @Override
   protected void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      mSpUtils = new SpUtils(this);
      int loginId = mSpUtils.getInt(SpUtils.LOGIN_KEY, -1);
      Log.d("", "login id = " + loginId);
      if (loginId >= 0) {
         Intent intent = new Intent(LoginActivity.this, MainActivity.class);
         intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
         startActivity(intent);
         return;
      }
      setContentView(R.layout.activity_login);
      mEtUsername = findViewById(R.id.et_student_username);
      mEtPassword = findViewById(R.id.et_student_password);
      mBtnRegister = findViewById(R.id.register_button);
      mBtnLogin = findViewById(R.id.login_button);
      mBtnRegister.setOnClickListener(this);
      mBtnLogin.setOnClickListener(this);

//      setContentView(R.id.iv_show_hide_password);

      // 初始化密码输入框和“眼睛”图标
      etStudentPassword = findViewById(R.id.et_student_password);
      ivShowHidePassword = findViewById(R.id.iv_show_hide_password);

      // 为“眼睛”图标设置点击事件监听器
      ivShowHidePassword.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            togglePasswordVisibility();
         }
      });


   }

   @Override
   public void onClick(View v) {
      int id = v.getId();
      if (id == R.id.register_button) { //点击注册按钮，跳转到注册界面
         Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
         startActivity(intent);
      } else if (id == R.id.login_button) { //点击登录按钮
         String username = mEtUsername.getText().toString();
         String password = mEtPassword.getText().toString();
         if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) { //用户名或密码没有输入，Toast提示。
            Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
            return;
         }
         //数据库中查询用户名和密码都匹配的数据
         Student student = DBHelper.getInstance(this).login(username, password);
         if (student == null) { //没有匹配的数据，登录失败，Toast提示。
            Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();
         } else {//登录成功跳转到主界面
            mSpUtils.putInt(SpUtils.LOGIN_KEY, student.getStudent_id());//记录当前登录学生的id
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
         }
      }
   }
}
